<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="permitBack"
          v-hasPermi="['output:cpmPermit:back']"
        >
          撤回
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-circle-check"
          size="mini"
          :disabled="multiple"
          @click="handlePermit"
          v-hasPermi="['output:cpmPermit:permit']"
        >
          批准
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['output:cpmStart:export']"
        >
          导出
        </el-button>
      </el-col>
      <HdTagGroup />

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      :height="tableH"
      v-loading="loading"
      :row-class-name="iCellStyle"
      :data="cpmStartList"
      @selection-change="handleSelectionChange"
      @row-click="clickRow"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" width="40" type="index" align="center" />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="退货单号"
        :show-overflow-tooltip="true"
        width="135"
        align="center"
        prop="cpmDjbh"
      />
      <el-table-column
        label="退货日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="cpmCtrq"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.cpmCtrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="退货班组"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cpmCsdm"
      ></el-table-column>
      <el-table-column
        label="仓库"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cpmCkdm"
      >
        <template #default="scope">
          <dict-tag :options="allStorageOptions" :value="scope.row.cpmCkdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="撤回原因"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cpmNgdesc"
      />
      <el-table-column
        label="退货状态"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cpmZtbz"
      >
        <template #default="scope">
          <dict-tag :options="cpmZtbzOptions" :value="scope.row.cpmZtbz" />
        </template>
      </el-table-column>
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cpmJlry"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="cpmJlrq"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.cpmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cpmShry"
      />
      <el-table-column
        label="审核日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="cpmShrq"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.cpmShrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="批准人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cpmPzry"
      />
      <el-table-column
        label="批准日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="cpmPzrq"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.cpmPzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="过账人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cpmGzry"
      />
      <el-table-column
        label="过账日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="cpmGzrq"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.cpmGzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!--明细-->
    <el-tabs v-model="activeTab">
      <el-tab-pane label="明细" name="first">
        <el-table
          border
          stripe
          :height="tableHAddChild"
          :data="cpdList"
          id="exportMx"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" align="center" fixed="left" />
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            width="135"
            align="center"
            prop="cpdWldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="申请数量"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="cpdSqsl"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="领料统计" name="second">
        <el-table border stripe :height="tableHAddChild" :data="cpdListTotal">
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            width="135"
            align="center"
            prop="cpdWldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            width="400"
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="申请数量"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="cpdSqsl"
          />
          <el-table-column
            label="不良原因"
            :show-overflow-tooltip="true"
            align="center"
            prop="cpdBlyy"
          />
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="680px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="70px"
      >
        <el-col :span="12">
          <el-form-item label="退货单号" prop="cpmDjbh">
            <el-input
              v-model="queryParams.cpmDjbh"
              placeholder="请输入退货单号"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" prop="cpmWldm">
            <el-input
              v-model="queryParams.cpmWldm"
              placeholder="请输入物料代码"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开单日期">
            <el-date-picker
              v-model="daterangeCpmJlrq"
              :picker-options="pickerOptions"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item>
            <RadioGroup v-model="queryParams.cpmZtbz">
              <Radio label="8" style="margin: 0px 30px">未审核</Radio>
              <Radio label="9" style="margin-right: 30px">已审核</Radio>
              <Radio label="7" align="right">全部</Radio>
            </RadioGroup>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="规格描述" prop="itmPmgg">
            <el-input
              v-model="queryParams.itmPmgg"
              placeholder="请输入规格描述"
              clearable
              style="width: 560px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCpmStart,
  permitBackCpm,
  permitCpm,
  exportCpmStart,
  getCpmStartDetail,
} from "@/api/output/cpm";
import { getAllCs } from "@/api/putStorage/putStorageAdd";
import { getAll } from "@/api/repertory/Storage";

export default {
  name: "CpmPermit",
  data() {
    return {
      //表格高度
      tableH: 250,
      tableHChild: 300,
      tableHAdd: 250,
      tableHAddChild: 300,
      cpdList: [],
      cpdListTotal: [],
      openChild: false,
      serachAddList: [],
      total2: 0,
      activeTab: "second",
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      cpmDjbhs: [],
      //选中的数据
      clickCpmList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      //选中的单据
      cpmMstrs: [],
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      //单位字典
      unitFlagOptions: [],
      //存放位置
      allCwOptions: [],
      allCsOptions: [],
      //退货日期
      cpmThrq: new Date(),
      // 退货状态字典
      cpmZtbzOptions: [
        { dictValue: "1", dictLabel: "新增", listClass: "default" },
        { dictValue: "2", dictLabel: "审核", listClass: "default" },
        { dictValue: "3", dictLabel: "批准", listClass: "default" },
        { dictValue: "6", dictLabel: "过账", listClass: "default" },
      ],
      //退货厂商
      cpmThCs: null,
      //退货厂商单号
      vCpmDjbhs: [],
      // 总条数
      total: 0,
      // 退货出库表格数据
      cpmStartList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示厂商确认
      vConfirmOpen: false,
      // 建立日期时间范围
      daterangeCpmJlrq: [
        this.$moment().startOf("month").toDate(),
        this.$moment().add(100, "year").endOf("month").toDate(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 40,
        cpmDjbh: null,
        cpmWldm: null,
        cpmJlrq: null,
        itmPmgg: null,
        cpmZtbz: "8",
      },
      // 表单参数
      form: {},
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    getAll().then((response) => {
      this.allCwOptions = response.data;

      for (let item2 of this.allCwOptions) {
        item2.dictLabel = item2.storageDefaultstorage;

        item2.listClass = "default";
        item2.dictValue = item2.storageId + "";
      }
    });
    getAllCs().then((response) => {
      this.allCsOptions = response.data;
      for (let item of this.allCsOptions) {
        item.dictLabel = item.csmCsmc;
        item.listClass = "default";
        item.dictValue = item.csmCsdm;
      }
    });
    this.getList();
  },
  methods: {
    /** 查询退货出库列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCpmJlrq && "" != this.daterangeCpmJlrq) {
        this.queryParams.params["beginCpmJlrq"] = this.moment(
          this.daterangeCpmJlrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endCpmJlrq"] = this.moment(
          this.daterangeCpmJlrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listCpmStart(this.queryParams).then((response) => {
        this.cpmStartList = response.rows;
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickRow(row);
        } else if (this.cpmStartList != null && this.cpmStartList.length > 0) {
          this.clickRow(this.cpmStartList[0]);
        } else {
          this.type1 = true;
        }
      });
    },
    /** 颜色事件 */
    iCellStyle({ row }) {
      //提交
      if (row.cpmZtbz == "1" && row.cpmTjflag == "1") {
        return "submit";
      }
      //审核
      else if (row.cpmZtbz == "2" && row.cpmTjflag == "1") {
        return "check";
      }
      //批准
      else if (row.cpmZtbz == "4" && row.cpmTjflag == "1") {
        return "permit";
      }
      //过账
      else if (row.cpmZtbz == "6" && row.cpmTjflag == "1") {
        return "past";
      }
    },
    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 40;
        this.queryParams.pageNum++;
        listCpmStart(this.queryParams).then((response) => {
          this.cpmStartList = this.cpmStartList.concat(response.rows);
          this.total = response.total;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    //厂商确认重置
    resetConfirm() {
      this.vCpmDjbhs = [];
      this.cpmThrq = new Date();
      this.cpmThCs = null;
    },
    // 表单重置
    reset() {
      this.form = {
        cpmId: null,
        cpmDjbh: null,
        cpmCtrq: null,
        cpmCsdm: null,
        cpmWldm: null,
        cpmCwdm: null,
        cpmCksl: null,
        cpmThyy: null,
        cpmNgdesc: null,
        cpmTjflag: null,
        cpmZtbz: null,
        cpmJlry: null,
        cpmJlrq: null,
        cpmShry: null,
        cpmShrq: null,
        cpmPzry: null,
        cpmPzrq: null,
        cpmGzry: null,
        cpmGzrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.cpmStartList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCpmJlrq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.cpmDjbhs = selection.map((item) => item.cpmDjbh);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.cpmMstrs = selection;
    },
    handleSearch() {
      this.reset();
      this.title = "查询退货出库";
      this.openSearch = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      exportCpmStart(queryParams)
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    //检验状态
    check_state() {
      for (let i of this.cpmMstrs) {
        if (i.cpmZtbz != 2 || i.cpmTjflag != 1) {
          return true;
        }
      }
      return false;
    },
    //批准单据
    handlePermit() {
      if (this.check_state()) {
        this.msgError("选中的单据未批准状态,无法进行批准");
      } else {
        permitCpm(this.cpmDjbhs)
          .then((res) => {
            this.msgSuccess("批准成功");
            this.getList();
          })
          .catch(() => {
            this.msgError("批准单据失败~");
          });
      }
    },
    //撤回单据
    permitBack() {
      if (this.check_state()) {
        this.msgError("选中的单据未批准状态,无法进行撤回");
      } else {
        permitBackCpm(this.cpmDjbhs)
          .then((res) => {
            this.getList();
            this.msgSuccess("撤回成功");
          })
          .catch(() => {
            this.msgError("撤回单据失败~");
          });
      }
    },
    // 获取明细
    getRowDetList(order) {
      getCpmStartDetail(order).then((response) => {
        this.cpdList = response.data.list;
        this.cpdListTotal = response.data.total;
        this.mxDate = this.cpdList.length > 0 ? false : true;
      });
    },

    clickRow(row) {
      // //获得选中的详细数据
      this.rowData = row;
      // //获得当前单据的状态
      this.id = row.cpmId;
      this.type1 = !(row != null && row.cpmZtbz == "1" && row.cpmTjflag == "0");
      //获得选中的详细数据
      this.getRowDetList(row.cpmDjbh);
    },
  },
};
</script>
<style lang="scss" scoped>
.inputName > .el-input__inner {
  height: 20px;
  line-height: 20px;
}
</style>
